package com.vviton.zyhj.admin.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.vviton.zyhj.common.base.AppConfig;
import com.vviton.zyhj.common.common.ValidatorConstants;
import com.vviton.zyhj.common.content.SpringContext;
import com.vviton.zyhj.common.utils.FileUtils;
import com.vviton.zyhj.common.utils.HttpClientUtil;
import com.vviton.zyhj.common.utils.ResultJson;

/**
 * ClassName: UploadFileServlet <br/>
 * Function: 文件上传Servlet. <br/>
 * Date: 2016年11月10日 下午2:21:46 <br/>
 *
 * @author weiming.chen
 * @version 1.0
 * @since JDK 1.7
 */
public class UploadFileServlet extends HttpServlet {

    /** 序列号 */
    private static final long serialVersionUID = 1L;

    public UploadFileServlet() {
        super();
    }

    /**
     * doGet,文件上传get请求. <br/>
     * Author: huangqq <br/>
     * Create Date: 2016年2月1日 <br/>
     * =============================================================== <br/>
     * Modifier: huangqq <br/>
     * Modify Date: 2016年2月1日 <br/>
     * Modify Description: <br/>
     * =============================================================== <br/>
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws ServletException 异常
     * @throws IOException 异常
     * @since JDK 1.7
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * doPost,文件上传post请求. <br/>
     * Author: huangqq <br/>
     * Create Date: 2016年2月1日 <br/>
     * =============================================================== <br/>
     * Modifier: huangqq <br/>
     * Modify Date: 2016年2月1日 <br/>
     * Modify Description: <br/>
     * =============================================================== <br/>
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws ServletException 异常
     * @throws IOException 异常
     * @since JDK 1.7
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultJson rj = new ResultJson(false);
        long limitSize = 1024 * 1024 * 15L;
        String mes = "{\"msgObj\":\"file size limit exceed\",\"successFlg\":false}";
        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("UTF-8");
            if (!ServletFileUpload.isMultipartContent(request)) {
                return;
            }
            List<FileItem> list = upload.parseRequest(request);
            List<FileItem> tempList = new ArrayList<FileItem>();
            for (FileItem item : list) {
                if (item.isFormField()) {
                    String value = item.getString("UTF-8");
                    limitSize = Long.valueOf(value);
                } else {
                    tempList.add(item);
                }
            }
            for (FileItem item : tempList) {
                String filename = item.getName();
                if (filename == null || filename.trim().equals("")) {
                    continue;
                }
                InputStream in = item.getInputStream();
                if (item.getSize() - limitSize > 0) {
                    mes = "{\"msgObj\":\"file size limit exceed\",\"successFlg\":false,\"code\":\"E9002\"}";
                    rj.setCode(ValidatorConstants.E9002);
                    break;
                } else {
                    File file = FileUtils.saveTempFileByInputStream(in, filename); //UUID.randomUUID().toString()
                    AppConfig appConfig = SpringContext.getBean(AppConfig.class);
                    ResultJson ret = HttpClientUtil.httpPostFile(appConfig.getDfsUploadUrl(), file);
                    if (ret.isResult()) {
                        rj.setResult(true);
                        mes = ret.getMessage();
                    }
                    item.delete();
                    file.delete();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        response.getWriter().write(mes); //rj.toString()
    }
}
